﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using HdipCricketClub.ViewModels;


namespace HdipCricketClub.Models
{
    [MetadataType(typeof(ScorecardHeaderMetaData))]
    public partial class ScorecardHeader
    {
        CricketClubEntities db = new CricketClubEntities();

        public ScorecardHeader ParseFormCollection(FormCollection formCollection)
        {
            string[] fixtureId = formCollection.GetValues("ScorecardScorecardHeader.fixture_id");
            string[] resultId = formCollection.GetValues("ScorecardScorecardHeader.result_id");
            string[] wonToss = formCollection.GetValues("ScorecardScorecardHeader.won_toss");
            string[] battedFirst = formCollection.GetValues("ScorecardScorecardHeader.batted_first");
            string[] batted = formCollection.GetValues("ScorecardScorecardHeader.Batted");
            string[] bowled = formCollection.GetValues("ScorecardScorecardHeader.Bowled");

            ScorecardHeader scorecardHeader = new ScorecardHeader()
                                            {
                                                fixture_id = Int32.Parse(fixtureId[0]),
                                                result_id = Int32.Parse(resultId[0]),
                                                won_toss = bool.Parse(wonToss[0]),
                                                batted_first = bool.Parse(battedFirst[0]),
                                                batted = bool.Parse(batted[0]),
                                                bowled = bool.Parse(bowled[0])
                                            };

            return scorecardHeader;
        }

        public void Update(FormCollection formCollection)
        {
            string[] fixtureId = formCollection.GetValues("ScorecardHeaderDetail.fixture_id");
            string[] resultId = formCollection.GetValues("ScorecardHeaderDetail.result_id");
            string[] wonToss = formCollection.GetValues("ScorecardHeaderDetail.won_toss");
            string[] battedFirst = formCollection.GetValues("ScorecardHeaderDetail.batted_first");
            string[] batted = formCollection.GetValues("ScorecardHeaderDetail.Batted");
            string[] bowled = formCollection.GetValues("ScorecardHeaderDetail.Bowled");

            ScorecardHeader scorecardHeader = new ScorecardHeader()
            {
                fixture_id = Int32.Parse(fixtureId[0]),
                result_id = Int32.Parse(resultId[0]),
                won_toss = bool.Parse(wonToss[0]),
                batted_first = bool.Parse(battedFirst[0]),
                scorecard_entered = true,
                batted = bool.Parse(batted[0]),
                bowled = bool.Parse(bowled[0])
            };

            db.ScorecardHeaders.AddObject(scorecardHeader);
            db.SaveChanges();
        }

        public string Validate(ScorecardAbandonedFixtureDetailViewModel scorecardAbandonedFixtureDetailViewModel)
        {
            string errorMessage = "";

            if ((scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail.batted != true) && (scorecardAbandonedFixtureDetailViewModel.ScorecardHeaderDetail.bowled != true))
            {
                errorMessage = "You must select either batted or bowled, or both";
            }

            return errorMessage;
        }

        public void UpdateScorecardHeaderDetails(ScorecardHeader scorecardHeaderDetail)
        {
            ScorecardHeader scorecardHeader = new ScorecardHeader();

            scorecardHeader.fixture_id = scorecardHeaderDetail.fixture_id;
            scorecardHeader.result_id = scorecardHeaderDetail.result_id;
            scorecardHeader.won_toss = scorecardHeaderDetail.won_toss;
            scorecardHeader.batted_first = scorecardHeaderDetail.batted_first;
            scorecardHeader.batted = scorecardHeaderDetail.batted;
            scorecardHeader.bowled = scorecardHeaderDetail.bowled;
            scorecardHeader.scorecard_entered = true;

            db.ScorecardHeaders.AddObject(scorecardHeader);
            db.SaveChanges();
        }

        public void DeleteFixtureHeaderDetails(int Id)
        {
            ScorecardHeader scorecardHeader = db.ScorecardHeaders.Where(sh => sh.fixture_id == Id).Single();

            db.ScorecardHeaders.DeleteObject(scorecardHeader);
            db.SaveChanges();
        }

    }

    public class ScorecardHeaderMetaData
    {
        [Required(ErrorMessage = "You must enter a result type")]
        public int result_id;

        public bool won_toss;

        public bool batted_first;

        public bool scorecard_entered;

        public bool batted;

        public bool bowled;
    }



}